package com.zwq.wxpay.sdk;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XMLUtil {

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

        if(null == strxml || "".equals(strxml)) {
            return null;
        }

        Map m = new HashMap();

        InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(in);
        Element root = doc.getRootElement();
        List list = root.getChildren();
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Element e = (Element) it.next();
            String k = e.getName();
            String v = "";
            List children = e.getChildren();
            if(children.isEmpty()) {
                v = e.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }

            m.put(k, v);
        }

        //关闭流
        in.close();

        return m;
    }

    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    public static String getChildrenText(List children) {
        StringBuffer sb = new StringBuffer();
        if(!children.isEmpty()) {
            Iterator it = children.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                sb.append("<" + name + ">");
                if(!list.isEmpty()) {
                    sb.append(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }

        return sb.toString();
    }


    public static void main(String[] args) throws IOException, JDOMException {

        String body = "\n" +
                "<xml>\n" +
                "<ToUserName><![CDATA[gh_0dbd96c7a630]]></ToUserName>\n" +
                "<FromUserName><![CDATA[o-_pI50AcwRZtw_6p6nxE4GSKC8U]]></FromUserName>\n" +
                "<CreateTime>1727104353</CreateTime>\n" +
                "<MsgType><![CDATA[event]]></MsgType>\n" +
                "<Event><![CDATA[SCAN]]></Event>\n" +
                "<EventKey><![CDATA[123]]></EventKey>\n" +
                "<Ticket><![CDATA[gQGw8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVjRVb1lTWFdkOEQxVVBUV2hDMUoAAgSzfPFmAwSAOgkA]]></Ticket>\n" +
                "</xml>";

        /*
            <xml>
                <ToUserName><![CDATA[gh_0dbd96c7a630]]></ToUserName>
                <FromUserName><![CDATA[o-_pI51Dl7UKGHrjvYolDjONdwsc]]></FromUserName>
                <CreateTime>1727106522</CreateTime>
                <MsgType><![CDATA[event]]></MsgType>
                <Event><![CDATA[SCAN]]></Event>
                <EventKey><![CDATA[123]]></EventKey>
                <Ticket><![CDATA[gQGw8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVjRVb1lTWFdkOEQxVVBUV2hDMUoAAgSzfPFmAwSAOgkA]]></Ticket>
            </xml>
         */

        // gQGw8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyVjRVb1lTWFdkOEQxVVBUV2hDMUoAAgSzfPFmAwSAOgkA

        Map map = doXMLParse(body);
        int size = map.size();
        System.out.println("size = " + size);

        if (size > 0) {
            String ToUserName = (String) map.get("ToUserName");
            System.out.println("ToUserName = " + ToUserName);
        }

    }
}
